package IOperator;

import Book.*;
import FileOperation.saveBooks;
import java.sql.SQLException;
import java.util.Scanner;
public class DelBooks implements IOperator {
    @Override
    public void work(Bookshelf bookshelf) throws SQLException {
        System.out.println("删除图书!");
        System.out.print("请输入你要删除的书籍# ");
        Scanner scanner = new Scanner(System.in);
        String bookName = scanner.nextLine();

        int index = -1;//记录该图书的下标位置
        int currCount = bookshelf.getBooksNumber();
        for (int i = 0; i < currCount; i++) {
            Book book = bookshelf.getBooks(i);
            if (bookName.equals(book.getName())) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            //没有找到该图书
            System.out.println("没有找到《" + bookName + "》!");
            return;
        }
        bookshelf.Remove(index);
        //将最后一本书的位置引用制空
        //bookshelf.setBooks(currCount - 1, null);
        new saveBooks().fileSaveCover(bookshelf);
        System.out.println("成功删除《" + bookName + "》!");
    }
}
